cmake_minimum_required(VERSION 3.16)
project(VimLVM VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 编译选项
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -DDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
set(CMAKE_BUILD_TYPE Debug)

# 包含目录
include_directories(include)

# 检查是否存在本地googletest
if(EXISTS "${CMAKE_SOURCE_DIR}/thirdparty/googletest")
    add_subdirectory(thirdparty/googletest)
    set(BUILD_TESTS ON)
else()
    message(WARNING "GoogleTest not found locally, skipping tests")
    set(BUILD_TESTS OFF)
endif()

# 源文件
set(VIML_SOURCES
    src/viml/value.cpp
    src/viml/instruction.cpp
    src/viml/vm.cpp
    src/viml/debugger.cpp
    src/viml/debug_info.cpp
    src/viml/compiler.cpp
    src/viml/gc.cpp
    src/viml/optimizer.cpp
)

# 主库
add_library(vimlvm_core STATIC
    src/viml/vm.cpp
    src/viml/value.cpp
    src/viml/instruction.cpp
    src/viml/compiler.cpp
    src/viml/debugger.cpp
    src/viml/debug_info.cpp
    src/viml/gc.cpp
    src/viml/optimizer.cpp
)

# 可执行文件
add_executable(vimlvm src/main.cpp)
target_link_libraries(vimlvm vimlvm_core)

# 调试器可执行文件
add_executable(vimlvm_debug src/debug_cli.cpp)
target_link_libraries(vimlvm_debug vimlvm_core readline)

# 测试
if(BUILD_TESTS)
    add_executable(vimlvm_test
        test/viml/test_main.cpp
        test/viml/test_vm.cpp
        test/viml/test_value.cpp
        test/viml/test_instruction.cpp
        test/viml/test_compiler.cpp
        test/viml/test_debugger.cpp
        test/viml/test_gc.cpp
        test/viml/test_compiler_basic.cpp
        test/viml/test_for_loop.cpp
        test/viml/test_optimizer.cpp
    )
    target_link_libraries(vimlvm_test vimlvm_core gtest gtest_main pthread)
    enable_testing()
    add_test(NAME VimLVMTest COMMAND vimlvm_test)
endif()